package com.spring.sunflower.userinfo;

import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.common.CommonContract;
import com.spring.sunflower.constant.Constant;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Kevin on 2021/6/30<br/>
 * <p>
 * Describe:<br/>
 */
public class SelfSignatureEditActivity extends BaseActivity<NickNameEditPresenter> implements CommonContract.NicknameEditView {
    private EditText etSignature;
    private TextView tvTextNum;
    private InputFilter filter = (source, start, end, dest, dstart, dend) -> {
        if (source.equals(" ")) return "";
        else return null;
    };

    @Override
    public void initView() {
        Intent intent = getIntent();
        String signature = intent.getStringExtra(Constant.USER_SIGNATURE);
        tvTitle.setText("交友心声");
        tvFunctionX.setText("保存");
        etSignature = findViewById(R.id.etSignature);
        etSignature.setText(signature);
        etSignature.setSelection(signature.length());
        etSignature.setFilters(new InputFilter[]{filter});
        tvTextNum = findViewById(R.id.tvTextNum);
        tvTextNum.setText(signature.length() + "/40");
//        ivClear.setVisibility(View.INVISIBLE);
        etSignature.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                printD("start=" + start + ",before=" + before + ",count=" + count);
            }

            @Override
            public void afterTextChanged(Editable s) {
                int length = etSignature.getText().toString().trim().length();
                tvTextNum.setText(length + "/40");
                if (length > 40) {
                    tvTextNum.setTextColor(getColorX(R.color.red_2));
                } else {
                    tvTextNum.setTextColor(getColorX(R.color.gray_6));

                }
            }
        });

        tvFunctionX.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nickname = etSignature.getText().toString().trim();
                if (TextUtils.isEmpty(nickname)) {
                    showToast("交友心声不能为空");
                } else {
                    if (nickname.length() > 40) {
                        showToast("交友心声字数大于40");
                        return;
                    }
                    Map<String, String> map = new HashMap<>();
                    map.put("signature", nickname);
                    mPresenter.updateUserInfo(map);
                }
            }
        });

    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_self_signature;
    }

    @Override
    public NickNameEditPresenter setPresenter() {
        return new NickNameEditPresenter(this);
    }

    @Override
    public void updateSuccess() {
        Intent intent = new Intent(this, UserInfoEditActivity.class);
        intent.putExtra(Constant.USER_SIGNATURE, etSignature.getText().toString().trim());
        setResult(RESULT_OK, intent);
        showToast("保存成功");
        finish();

    }

    @Override
    public void updateFailed() {
        showToast("保存失败:(");
    }
}
